package com.future.offer;

import com.future.util.TreeNode;

/**
 * Description: 剑指 Offer 28. 对称的二叉树
 *
 * @author weiruibai.vendor
 * Date: 2022/9/28 11:28
 */
public class Solution_28 {

    public static void main(String[] args) {

        Solution_28 solution_028 = new Solution_28();
        TreeNode root = new TreeNode(1);
        root.left = new TreeNode(2);
        root.right = new TreeNode(2);
        root.left.right = new TreeNode(3);
        root.right.right = new TreeNode(3);
        System.out.println(solution_028.isSymmeric(root));
    }

    /**
     * ---- 1
     * ----/ \
     * --2   2
     * ---\   \
     * -- 3    3
     *
     * @param root
     * @return
     */
    public boolean isSymmeric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return process(root.left, root.right);
    }

    private boolean process(TreeNode left, TreeNode right) {
        if (left == null ^ right == null) {
            return false;
        }
        if (left == null && right == null) {
            return true;
        }
        boolean b = false;
        if (left.val == right.val) {
            b = process(left.left, right.right);
            if (!b) {
                return false;
            }
            b = process(left.right, right.left);
        }
        return b;
    }


}
